/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



package itm.conexion;

//~--- non-JDK imports --------------------------------------------------------

import com.microsoft.sqlserver.jdbc.SQLServerConnection;
import com.microsoft.sqlserver.jdbc.SQLServerStatement;
import itm.exception.ControlException;

import static itm.constants.DatabaseConstants.*;

//~--- JDK imports ------------------------------------------------------------

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import javax.sql.DataSource;

/**
 *
 * @author Administrador
 */
public class SQLServerConection {
   // Connection    dbConnection = null;
    DataSource    ds           = null;
    Context       envContext   = null;
    Context       initContext  = null;
    public String resEx;
    private SQLServerConnection dbConnection;

   

     public SQLServerConnection openConnection(){
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            try {
                //dbConnection = (SQLServerConnection) DriverManager.getConnection("jdbc:sqlserver://172.30.0.45:1433;databaseName=ITM_CENINV", "itm_bisa", "bisacorp");
              //  dbConnection = (SQLServerConnection) DriverManager.getConnection("jdbc:sqlserver://192.168.1.115:1433;databaseName=ITM_CENINV", "sa", "bisacorp");
                dbConnection = (SQLServerConnection) DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=ITM_CENINV", "sa", "bisacorp");
           } catch (SQLException ex) {
                ex.printStackTrace();
                Logger.getLogger(SQLServerConection.class.getName()).log(Level.SEVERE, null, ex);
          }
          } catch (ClassNotFoundException ex) {
            Logger.getLogger(SQLServerConection.class.getName()).log(Level.SEVERE, null, ex);
         }
       return dbConnection;
  }

      public Connection openTransaccion() throws ControlException {
        try {
            openConnection();
            dbConnection.setAutoCommit(false);
            return dbConnection;
        }catch(SQLException e) {
            e.printStackTrace();
            throw new ControlException("No se inicializo la transacci�n" + e.getMessage());
        }
  }
	public void commit() throws ControlException{
		if(dbConnection == null){
			throw new ControlException("La conexi�n no se ha inicializado.  Se debe iniciar con begin().");
		}
		try {
			dbConnection.commit();
			dbConnection.setAutoCommit(true);
		    dbConnection.close();
		}
		catch (SQLException e) {
		    throw new ControlException(e.getMessage());
		}
	}

	public void rollback() throws ControlException{
	    if(dbConnection == null){
	        throw new ControlException("La conexi�n no se ha inicializado.  Se debe iniciar con begin().");
	    }
		try {
			dbConnection.rollback();
			dbConnection.setAutoCommit(true);
			dbConnection.close();
		}
		catch (SQLException e) {
		    throw new ControlException(e.getMessage());
		}
	}

    public void closeConnection(Connection conexion) {
        try {
            conexion.close();
        } catch (SQLException sqle) {
            sqle.getStackTrace();
        }
    }

    

    public String getResEx() {
        return resEx;
    }

    public void setResEx(String resEx) {
        this.resEx = resEx;
    }
}
